package com.bzr.emos.wx.util;

import io.jsonwebtoken.*;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;

public class JwtUtils {

    private static String SECRET_KEY="nXfzD68Cj0aUjq6a2ndbkwSqeGWGMO7ZMmSxuawJbAbk7TYFyJ05BsBVO0Omj3y0qJntWn3AEgXuYroPG+XhtA==";
    private static final long EXPIRATION_TIME = 86400000; // 24小时





    public static String generateToken(String userId) {
        System.out.println("KEY: " + SECRET_KEY);
        return Jwts.builder()
                .setSubject(userId)
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
                .signWith(SignatureAlgorithm.HS512, SECRET_KEY)
                .compact();
    }

    public static String validateToken(String token) {
        try {
            Claims claims = Jwts.parser()
                    .setSigningKey(SECRET_KEY)
                    .parseClaimsJws(token)
                    .getBody();
            return claims.getSubject();
        } catch (ExpiredJwtException | UnsupportedJwtException | MalformedJwtException | IllegalArgumentException e) {
            // 处理异常，如token过期或无效
            throw new RuntimeException("Invalid JWT token");
        }
    }
}